<?php

  class Error {
    
    var $code;
    var $level;
    var $mode;
    var $message;
    var $userinfo;
    var $backtrace;
    
    static $mdb2_codes = array (
                  MDB2_OK =>                       true,
                  MDB2_ERROR =>                      -1,
                  MDB2_ERROR_SYNTAX =>                3,
                  MDB2_ERROR_CONSTRAINT =>           -3,
                  MDB2_ERROR_NOT_FOUND =>            -4,
                  MDB2_ERROR_ALREADY_EXISTS =>       -5,
                  MDB2_ERROR_UNSUPPORTED =>          -6,
                  MDB2_ERROR_MISMATCH =>             -7,
                  MDB2_ERROR_INVALID =>              -8,
                  MDB2_ERROR_NOT_CAPABLE =>          -9,
                  MDB2_ERROR_TRUNCATED =>           -10,
                  MDB2_ERROR_INVALID_NUMBER =>      -11,
                  MDB2_ERROR_INVALID_DATE =>        -12,
                  MDB2_ERROR_DIVZERO =>             -13,
                  MDB2_ERROR_NODBSELECTED =>        -14,
                  MDB2_ERROR_CANNOT_CREATE =>       -15,
                  MDB2_ERROR_CANNOT_DELETE =>       -16,
                  MDB2_ERROR_CANNOT_DROP =>         -17,
                  MDB2_ERROR_NOSUCHTABLE =>         -18,
                  MDB2_ERROR_NOSUCHFIELD =>           3,
                  MDB2_ERROR_NEED_MORE_DATA =>      -20,
                  MDB2_ERROR_NOT_LOCKED =>          -21,
                  MDB2_ERROR_VALUE_COUNT_ON_ROW =>  -22,
                  MDB2_ERROR_INVALID_DSN =>         -23,
                  MDB2_ERROR_CONNECT_FAILED =>      -24,
                  MDB2_ERROR_EXTENSION_NOT_FOUND => -25,
                  MDB2_ERROR_NOSUCHDB =>            -26,
                  MDB2_ERROR_ACCESS_VIOLATION =>    -27,
                  MDB2_ERROR_CANNOT_REPLACE =>      -28,
                  MDB2_ERROR_CONSTRAINT_NOT_NULL =>   4,
                  MDB2_ERROR_DEADLOCK =>            -30,
                  MDB2_ERROR_CANNOT_ALTER =>        -31,
                  MDB2_ERROR_MANAGER =>             -32,
                  MDB2_ERROR_MANAGER_PARSE =>       -33,
                  MDB2_ERROR_LOADMODULE =>          -34,
                  MDB2_ERROR_INSUFFICIENT_DATA =>   -35);
    
    static $php_codes = array (
                  E_ALL =>                8, // - All errors and warnings (doesn't include E_STRICT)
                  E_ERROR =>              2, // - fatal run-time errors
                  E_RECOVERABLE_ERROR =>  8, // - almost fatal run-time errors
                  E_WARNING =>            4, // - run-time warnings (non-fatal errors)
                  E_PARSE =>              3, // - compile-time parse errors
                  E_NOTICE =>             5, // - run-time notices (these are warnings which often result
                                            //  from a bug in your code, but it's possible that it was
                                            //   intentional (e.g., using an uninitialized variable and
                                            //   relying on the fact it's automatically initialized to an
                                            //   empty string)
                  E_STRICT =>             8, // - run-time notices, enable to have PHP suggest changes
                                            //   to your code which will ensure the best interoperability
                                            //   and forward compatibility of your code
                  E_CORE_ERROR =>         3, // - fatal errors that occur during PHP's initial startup
                  E_CORE_WARNING =>       4, // - warnings (non-fatal errors) that occur during PHP's
                                            //   initial startup
                  E_COMPILE_ERROR =>      3, // - fatal compile-time errors
                  E_COMPILE_WARNING =>    4, // - compile-time warnings (non-fatal errors)
                  E_USER_ERROR =>         3, // - user-generated error message
                  E_USER_WARNING =>       4, // - user-generated warning message
                  E_USER_NOTICE =>        5);// - user-generated notice message
    
    
    
    
    
    
    function __construct () {
    }
    
    
    
    
    
    static function format_backtrace($_backtrace) {
      if ($_backtrace != '') {
        foreach ($_backtrace as $number => $val) {
          $backtrace[$number][] = str_pad(substr($val['class'], 0, 20), 20);
          $backtrace[$number][] = str_pad(substr($val['function'], 0, 28), 28);
          $backtrace[$number][] = str_pad($val['line'], 5, ' ', STR_PAD_LEFT);
          $backtrace[$number][] = $val['file'];
          $backtrace[$number] = implode (' ', $backtrace[$number]);
        }
      }
      return $backtrace;
    }
    
    static function raise_error($message, $code) {
      $error = new Error();
      $error->message = $message;
      $error->code = $code;
      if ($code < 5)
        $error->backtrace = debug_backtrace();
      self::handle_error($error);
    }
    
    static function handle_php_error($code, $message, $file, $line, $errcontext) {
      $error = new Error();
      $error->message = $message;
      $error->code = self::$php_codes[$code];
      $error->backtrace = debug_backtrace();
      self::handle_error($error);
    }
    
    
    static function handle_mdb2_error ($mdb2_error) {
      $error = new Error();
      $error->mode = $mdb2_error->mode;
      $error->level = $mdb2_error->level;
      $error->message = $mdb2_error->message;
      $error->code = self::$mdb2_codes[$mdb2_error->code];
      if ($error->code < 0) {
        $error->message .= " [CODE: {$error->code}]";
        $error->code = 3;
      }
      preg_match_all ('/\[(.*): (.*)\]\n/', $mdb2_error->userinfo, $matches);
      $error->userinfo = array_combine ($matches[1], $matches[2]);
      $error->backtrace = $mdb2_error->backtrace;
      self::handle_error($error);
    }
    

    static function handle_mdb2_debug ($driver_object, $type, $query, $debug) {
      switch ($type) {
        case 'query':
          $query = preg_replace ('/^PREPARE MDB2_STATEMENT_.* FROM \'/', '', $query);
          $message = "      [query  {$query}]";
          $GLOBALS['Akrudo']->logger->log($message, PEAR_LOG_INFO);
        break;
      }
    }
    
    
    static function handle_pear_error ($pear_error) {
      //array_shift($pear_error->backtrace);
      $error = new Error();
      $error->mode = $pear_error->mode;
      $error->level = $pear_error->level;
      $error->code = $pear_error->code;
      $error->message = $pear_error->message;
      $error->userinfo = $pear_error->userinfo;
      $error->backtrace = $pear_error->backtrace;
      self::handle_error($error);
    }
    
    
    static function handle_error ($error) {
      $error->backtrace = self::format_backtrace($error->backtrace);
      if ($error->code < 5) {
        $GLOBALS['Akrudo']->logger->log(escl($error), $error->code);
        //die();
      } else {
        //$message = preg_replace('/\n$/', '', escl($error->message));
        $GLOBALS['Akrudo']->logger->log($error->message, $error->code);
      }
      if ($error->code < 4) {
        die();
      }
      
      
      
    } 
  }
  
?>